package com.wtp.wallpaper.aspect;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtp.wallpaper.aspect.processor.ProcessorFactory;
import com.wtp.wallpaper.pojo.result.Result;
import com.wtp.wallpaper.utils.AliOssUtil;
import io.netty.util.internal.StringUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Aspect
@Component
public class ReplaceURLAspect {

    @Autowired
    private AliOssUtil aliOssUtil;

    @Pointcut("execution(* com.wtp.wallpaper.controller.*.*.*(..)) && @annotation(com.wtp.wallpaper.annotation.ReplaceURL) " +
            "|| @within(com.wtp.wallpaper.annotation.ReplaceURL)")
    public void replaceURLPointCut(){}

    @AfterReturning(pointcut = "replaceURLPointCut()",returning = "result")
    public void afterReturningAdvice(JoinPoint joinPoint,Object result){


        if(result instanceof Result<?> res){
            if(res.getData() instanceof List<?> list){
                processItems(list);
            }else if (res.getData() instanceof Page<?> page){
                processItems(page.getRecords());
            }
        }

        /*if(result instanceof Result<?>){
            Result res = (Result) result;

            Object data = res.getData();
            if(data instanceof List || data instanceof Page){

                List list = List.of();
                if(data instanceof Page page){
                    list = page.getRecords();
                }else{
                    list = (List) data;
                }


                for (Object o : list) {

                    if(o instanceof PictureVO pictureVO){
                        String picurl = pictureVO.getPicurl();
                        if(StringUtil.isNullOrEmpty(picurl))continue;
                        String p1 = (String) redisTemplate.opsForValue().get(picurl);
                        if(p1 == null){
                            p1 = aliOssUtil.generatePresignedUrl(picurl);
                            redisTemplate.opsForValue().set(picurl,p1,2, TimeUnit.HOURS);
                        }
                        pictureVO.setPicurl(p1);

                        String smallpicurl = pictureVO.getSmallpicurl();
                        String p2 = (String) redisTemplate.opsForValue().get(smallpicurl);
                        if(p2 == null){
                            p2 = aliOssUtil.generatePresignedUrl(smallpicurl);
                            redisTemplate.opsForValue().set(smallpicurl,p2,2, TimeUnit.HOURS);
                        }
                        pictureVO.setSmallpicurl(p2);
                    }


                    if(o instanceof ClassifyVO classifyVO){
                        String picurl = classifyVO.getPicurl();
                        if(StringUtil.isNullOrEmpty(picurl))continue;
                        String p1 = (String) redisTemplate.opsForValue().get(picurl);
                        if(p1 == null){
                            p1 = aliOssUtil.generatePresignedUrl(picurl);
                            redisTemplate.opsForValue().set(picurl,p1,2, TimeUnit.HOURS);
                        }
                        classifyVO.setPicurl(p1);
                    }

                    if(o instanceof BannerPicVO bannerPicVO){
                        String picurl = bannerPicVO.getPicurl();
                        if(StringUtil.isNullOrEmpty(picurl))continue;
                        String p1 = (String) redisTemplate.opsForValue().get(picurl);
                        if(p1 == null){
                            p1 = aliOssUtil.generatePresignedUrl(picurl);
                            redisTemplate.opsForValue().set(picurl,p1,2, TimeUnit.HOURS);
                        }
                        bannerPicVO.setPicurl(p1);
                    }
                }
            }
        }*/
    }

    private void processItems(List<?> items) {

        items.forEach(item ->
                ProcessorFactory.getProcessor(item.getClass()) // 直接传递对象实例
                        .ifPresent(processor -> {
                            processor.process(processor.getSupportedType().cast(item), aliOssUtil::getCachedUrl);
                        })
        );

       /* items.forEach(item ->
                ProcessorFactory.getProcessor(item.getClass()) // 直接传递对象实例
                        .ifPresent(processor -> {
                            if (!processor.supports(item)) return;
                            UrlProcessor<PictureVO> p = (UrlProcessor<PictureVO>)processor;
                            if(item instanceof PictureVO pp)
                            p.process(pp, this::getCachedUrl);
                        })
        );*/

    }

}
